
package com.scs.application.modules.msg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.BaseController;
import com.scs.application.modules.msg.entity.Template;
import com.scs.application.modules.msg.request.TemplateQueryRequest;
import com.scs.application.modules.msg.service.TemplateService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 消息模板 controller
*
*/
@AllArgsConstructor
@RestController
@RequestMapping("/msg/templates")
public class TemplateController extends BaseController {

    private TemplateService templateService;

    @PostMapping
    public CommonResult add(@RequestBody @Valid Template entity) {
        boolean bSave = templateService.saveCascadeById(entity);
        return bSave ? CommonResult.ok("新增成功", entity) : CommonResult.error("新增失败");
    }

    @PutMapping
    public CommonResult update(@RequestBody @Valid Template entity) {
        boolean bUpdate = templateService.updateCascadeById(entity);
        return bUpdate ? CommonResult.ok("保存成功", entity) : CommonResult.error("保存失败");
    }

    @GetMapping("/page")
    public PageResult page(TemplateQueryRequest request) {
        IPage page = templateService.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(TemplateQueryRequest request) {
        List<Template> list = templateService.list(request);
        return CommonResult.ok("查询成功", list);
    }


    @DeleteMapping("/{id}")
    public CommonResult deleteById(@PathVariable("id") String id) {
        boolean bRemove = templateService.removeCascadeById(id);
        return bRemove ? CommonResult.ok("删除成功") : CommonResult.error("删除失败");
    }

}
